package com.may.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: spring-intepay-cashier
 * @description: 构建 curl 命令，模拟 http 请求
 * @author: Bobby.Ma
 * @create: 2020-08-14 13:56
 **/
public class CurlUtil {
    private final List<String> cmds = new ArrayList<>();
    //判断当前操作系统是不是windows
    private final Boolean isWin = System.getProperty("os.name").startsWith("Win");

    public static CurlUtil create() {
        CurlUtil curl = new CurlUtil();
        curl.cmds.add("curl");
        return curl;
    }

    public CurlUtil get(String host) {
        cmds.add("-X");
        cmds.add("GET");
        cmds.add(host);
        return this;
    }

    public CurlUtil post(String host) {
        cmds.add("-X");
        cmds.add("POST");
        cmds.add(host);
        return this;
    }

    /*
        特别提醒：header中的参数，win下用双引号包裹，linux下用单引号包裹。容器中不能有引号
    */
    public CurlUtil header(String header) {
        cmds.add("-H");
        if (isWin) {
            cmds.add("\"" + header + "\"");
        } else {
            cmds.add(header);
        }
        return this;
    }

    public CurlUtil header(String name, String value) {
        return header(name + ":" + value);
    }

    public CurlUtil basicAuth(String auth) {
        cmds.add("-u");
        if (isWin) {
            cmds.add("\"" + auth + "\"");
        } else {
            cmds.add(auth);
        }
        return this;
    }

    public CurlUtil basicAuth(String username, String password) {
        return basicAuth(username + ":" + password);
    }

    public CurlUtil contentType(String value) {
        return header("Content-Type", value);
    }

    /*
        特别提醒：请求体参数，win下用双引号包裹，linux下用单引号包裹，容器中不能有引号
    */
    public CurlUtil body(String jsonData) {
        cmds.add("-d");
        if (isWin) {
            cmds.add("\"" + jsonData + "\"");
        } else {
            cmds.add(jsonData);
        }
        return this;
    }

    public String execute() {
        ProcessBuilder processBuilder = new ProcessBuilder(cmds);
        StringBuilder builder = new StringBuilder();
        Process process = null;
        BufferedReader reader = null;
        try {
            process = processBuilder.start();
            reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (process != null) {
                process.destroy();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return builder.toString();
    }

    /**
     * @param
     * @Description: 构造后的命令行
     * @return: java.lang.String
     * @Author: Bobby.Ma
     * @Date: 2020/8/17 10:55
     */
    public String cmdLine() {
        return StringUtils.join(this.cmds, " ");
    }
}
